<template>
  <el-dialog :title="info.title" :visible.sync="visible" center width="1460px" :before-close="handleCancel">
    <div class="">
      <el-row>
        <el-col :span="24">
          <div class="page-filter mb10 fl">
            <el-form ref="filter" :model="filter" label-width="0">
              <el-form-item class="mr10 mb5 fl" prop="begin_time">
                <el-date-picker
                  v-model="filter.begin_time"
                  placeholder="开始时间"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="small"
                  clearable
                  style="width: 100%"
                />
              </el-form-item>
              <el-form-item class="mr10 mb5 fl" prop="end_time">
                <el-date-picker
                  v-model="filter.end_time"
                  placeholder="结束时间"
                  type="date"
                  format="yyyy-MM-dd"
                  value-format="yyyy-MM-dd"
                  size="small"
                  clearable
                  style="width: 100%"
                />
              </el-form-item>
              <el-form-item class="mr10 mb5 fl" style="width: 300px">
                <el-button size="small" @click="app.resetFields($refs['filter'].$parent,'filter')">清 空</el-button>
                <el-button type="primary" size="small" @click="getList(getParameter())">查 询</el-button>
              </el-form-item>
            </el-form>
          </div>
        </el-col>
      </el-row>
      <el-table
          v-loading="app.loading"
          class="search-table"
          :data="list"
          border
          :summary-method="getSummaries"
          show-summary
          fit
          highlight-current-row
          style="width: 100%;"
        >
          <el-table-column label="序号" align="center" width="56px">
            <template slot-scope="{$index}">
              {{ $index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="客户名称" width="220px" prop="lc_name" />
          <el-table-column label="项目名称" width="220px" prop="name" :formatter="paytypeFormatter" />
          <el-table-column label="PO单号" width="190px">
            <template slot-scope="{row}">
              {{ 'PO'+row.id }}
            </template>
          </el-table-column>
          <el-table-column label="下单日期" align="center" width="110px" prop="order_time" />
          <el-table-column label="订单金额 " align="right" width="240px">
            <template slot-scope="{row}">
              {{ app.toFix(row.trading_price, 2) }}
            </template>
          </el-table-column>
          <el-table-column label="付款方式" width="110px">
            <template slot-scope="{row}">
              <span v-for="(item, index) in row.payments" :key="index" class="mr10">
                <a style="color: blue" 
                @click="handleDialog({...{pro_id: row.id}, ...
                    {dRef: 'paytypes', dTitle: '项目付款方式', dType: 'edit', dField: dFieldPaytypes}})"
                >{{ item.v + ": " + item.s + "%" }}</a><br>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="客户联系人" width="120px" prop="lcc_name" />
          <el-table-column label="联系电话" width="160px" prop="lcc_phone" />
          <el-table-column label="备注" min-width="260px" prop="remark">
            <template slot-scope="{row}">
              <span v-if="row.remark">{{ row.remark }} <br></span>
              <span v-if="row.bid_remark">{{ row.bid_remark }} <br></span>
              <span v-if="row.progress_remark">{{ row.progress_remark }} <br></span>
              <span v-if="row.order_remark">{{ row.order_remark }} <br></span>
              <span v-if="row.contract_remark">{{ row.contract_remark }} <br></span>
              <span v-if="row.budget_remark">{{ row.budget_remark }} <br></span>
            </template>
          </el-table-column>
        </el-table>
        <pagination v-show="filter.pageAll>0" :total="filter.pageAll" :page.sync="filter.pageCurrent" :limit.sync="filter.pageNumber" @pagination="getList" />
    </div>
    <Paytypes ref="paytypes" :get-data="getList" :info="dialog" />
  </el-dialog>
</template>

<script>
import page from '@/utils/page'
import { pros, proTotalAmount } from '@/api/statistics/statistics'
import Paytypes from '@/views/business/components/order/components/paytypes-dialog'

export default {
  components: { Paytypes },
  extends: page,
  inject: ['app'],
  props: {
    info: {
      type: Object,
      default: null
    },
    reqList: {
      default: pros,
      type: Function
    }
  },
  data() {
    return {
      parameter: { type: 'order' },
      price: {},
      dFieldPaytypes: ['pro_id', 'trading_price'],
      yearAmount: 0,
      monthAmount: 0,
      quarters: [],
      months: []
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList(this.getParameter())
      }
    },
    getData: {
      type: Function,
      default: function() {}
    }
  },
  mounted() {
  },
  methods: {
    getParameter() {
      let month = this.info.edit.month
      if((this.filter.begin_time && this.filter.begin_time !== '') || (this.filter.end_time && this.filter.end_time !== '')) {
        month = ''
      }
      return {...this.parameter, ...this.filter, ...{month: month}}
    },
    paytypeFormatter(e, c) {
      const xshk = e.xshk
      const xshks = []
      if (xshk) {
        const array = xshk.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('#')
            const f = {}
            f.v = js[0]
            f.m = js[1]
            f.t = js[2]
            xshks.push(f)
          })
        }
      }
      e.xshks = xshks

      const fund = e.fund
      const funds = []
      if (fund) {
        const array = fund.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('#')
            const f = {}
            f.v = js[0]
            f.m = js[1]
            f.t = js[2]
            funds.push(f)
          })
        }
      }
      e.funds = funds

      const payment = e.payment
      const payments = []
      if (payment) {
        const array = payment.split(',')
        if (array.length > 0) {
          array.forEach(element => {
            const js = element.split('-')
            const f = {}
            f.v = js[0] || ''
            f.s = js[3] || ''
            payments.push(f)
          })
        }
      }
      e.payments = payments

      return e.name
    },
    getListAfter() {
      this.handleAmount()
    },
    handleAmount() {
      proTotalAmount(this.getParameter()).then(res => {
        this.price = res.info ? res.info : {}
      }).catch(err => {
        this.$message.error(err.msg)
      })
    },
    getSummaries() {
      const sums = []
      sums[0] = '合计'
      sums[1] = ''
      sums[2] = ''
      sums[3] = ''
      sums[4] = ''
      sums[5] = this.app.toFix(this.price.trading_price, 2)
      return sums
    }
  }
}
</script>

<style scoped>
</style>
